<?php 
class db
{
	private $host, $user, $password, $database;
	private $connection, $db;
	
	// class constructor
	public function __construct() {
		$this->host = "localhost";
		$this->user = "root";
		$this->password = "";
		$this->database = "addressbook3";
	}

	// class destructor
	function __destruct() {
		if ( $this->connection != NULL )
			mysql_close($this->connection);	
	}
	
	// function to connect to host and select a database to operate on
	private function getDbConnection() {
		// connect to the host 
		$this->connection = mysql_connect($this->host, $this->user, $this->password); 
		if ( !$this->connection ) {
			die('Could not connect to host ' . $this->host . "!");	
		}	
		
		//select a database
		$this->db = mysql_select_db($this->database, $this->connection);
		if ( !$this->db ) {
			die ('Could not find database addressBook.');	
		}		
	}
	
	public function sendSQLStatement($sqlQuery) {
		$this->getDbConnection();
		return mysql_query($sqlQuery);
	}
	
	public function validateUser($userId) {
		// call private function in same class
		$this->getDbConnection();		
		
		$select_query = "SELECT * FROM colleague WHERE email='" . $userId . "'";
		$result = mysql_query($select_query);
		$counts = mysql_num_rows($result);
		
		if ( $counts == 1) {
			// update signbook table	
			$insert_query = "INSERT INTO signbook (user) VALUES ('" . $userId ."')";
			mysql_query($insert_query);
			
			return TRUE;
		}
		else return FALSE;
	}
	
	public function updateSignBook($userId) {
		$this->getDbConnection();			
		$update_query = "UPDATE signbook SET signout=Now() WHERE user='". $userId . "' AND signout=0";
		mysql_query($update_query);
	}
}
?>